package com.net.oil.main.adapter;
/*
 * Created by lch on 2018/2/22 0022.
 */

import android.content.Context;
import android.content.Intent;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.http.AppUrl;
import com.net.oil.main.bean.HomePageInfoBean;
import com.net.oil.product.ProductListActivity;
import com.net.oil.util.StringUtil;
import com.net.oil.util.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ProductRecyclerViewAdapter extends RecyclerView.Adapter<ProductRecyclerViewAdapter.ViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<HomePageInfoBean.ProductCategoryBean> productList;

    public ProductRecyclerViewAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        productList = new ArrayList<>();
    }

    public void updateData(List<HomePageInfoBean.ProductCategoryBean> productList) {
        this.productList.clear();
        this.productList.addAll(productList);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.product_recycler_view_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final HomePageInfoBean.ProductCategoryBean productBean = productList.get(position);
        if (productBean != null){
            if (!StringUtil.isEmpty(productBean.getCatName())){
                holder.productName.setText(productBean.getCatName());
            }

            if (!StringUtil.isEmpty(productBean.getImage())){
                GlideUtil.getInstance().loadImage(context, holder.productLogo, AppUrl.getImageUrl(productBean.getImage()), R.drawable.product_err);
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //2018-9-25，去掉产品分类，从首页点击直接跳转到产品列表
                    /*Intent intent = new Intent(context, ProductClassActivity.class);
                    intent.putExtra(Constant.INTENT_PRODUCT_CLASS_ID, productBean.getCatId());
                    context.startActivity(intent);*/
                    Intent intent = new Intent(context, ProductListActivity.class);
                    intent.putExtra(Constant.INTENT_PRODUCT_CLASS_ID, productBean.getCatId());
                    context.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return productList == null ? 0 : productList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.product_logo)
        ImageView productLogo;
        @BindView(R.id.product_name)
        TextView productName;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
